<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet
  version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <!-- Output will be copy of input XML sorted by dn attribute  -->
  <xsl:output method="xml" indent="yes"/>
  <xsl:strip-space elements="*"/>

  <!--  Match all attributes and nodes that aren't the root node    -->
  <xsl:template match="@* | child::node()">
    <!--  Copy the passed element. . .   -->
    <xsl:copy>
      <!--  . . .and all of its attributes    -->
      <xsl:copy-of select="@*"/>
      <!--   . . .apply appropriate template to all children   -->
      <xsl:apply-templates>
      <!-- sort by dn -->
        <xsl:sort select="@dn"/>
      </xsl:apply-templates>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>
